//
//  GridView.m
//  NCMM
//
//  Created by McCallie, Cameron D (Genworth) on 10/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GridView.h"


@implementation GridView
@synthesize rows;
@synthesize cols;
@synthesize offset;
@synthesize primary;
@synthesize secondary;


- (void)createPuzzle
{
    NSLog(@"createPuzzle called");
    NSLog(@"%d rows", rows);
    NSLog(@"%d cols", cols);
    
    
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            //TODO: Replace constants
            
            CGRect frame = CGRectMake((i*offset), (j*offset), offset, offset);
            PuzzleCharacter *current = [[PuzzleCharacter alloc] initWithLabel:[self randomLetter] andFrame:frame andDelegate:self];

            [self addSubview:current];
            [current release];
        }
    }
}



- (id)initWithSize:(NSString*)size andFrame:(CGRect)frame andDelegate:(id)delegate
{
    // Replace the two size dimensions when selecting size is implemented
    
    [super initWithFrame:frame];
    myDelegate = delegate;
    
    self.backgroundColor = [UIColor redColor];

    
    // Defaults to "medium" size
    
    rows = 20;
    cols = 20;
    offset = 30;
    
    if ([size isEqualToString:@"small"])
    {
        rows = 15; cols = 15;
    }
    else if ([size isEqualToString:@"medium"])
    {
        rows = 20; cols = 20;
    }
    else if ([size isEqualToString:@"large"])
    {
        rows = 25; cols = 25;
    }
    else
        NSLog(@"Failure in initWithSize");
    
    [self createPuzzle];
    return self;
}

- (NSString*)letterFrequency
{
    // http://en.wikipedia.org/wiki/Letter_frequency
    
    NSString *letters = [NSString stringWithString:@""];
    letters = [letters stringByAppendingString:@"AAAAAAAA"];        // A: 8.167
    letters = [letters stringByAppendingString:@"BB"];              // B: 1.492
    letters = [letters stringByAppendingString:@"CCC"];             // C: 2.782
    letters = [letters stringByAppendingString:@"DDDD"];            // D: 4.235
    letters = [letters stringByAppendingString:@"EEEEEEEEEEEEE"];   // E: 12.702
    letters = [letters stringByAppendingString:@"FF"];              // F: 2.228
    letters = [letters stringByAppendingString:@"GG"];              // G: 2.015
    letters = [letters stringByAppendingString:@"HHHHHH"];          // H: 6.094
    letters = [letters stringByAppendingString:@"IIIIIII"];         // I: 6.966
    letters = [letters stringByAppendingString:@"J"];               // J: 0.153
    letters = [letters stringByAppendingString:@"K"];               // K: 0.772
    letters = [letters stringByAppendingString:@"LLLL"];            // L: 4.025
    letters = [letters stringByAppendingString:@"MMM"];             // M: 2.406
    letters = [letters stringByAppendingString:@"NNNNNNN"];         // N: 6.749
    letters = [letters stringByAppendingString:@"OOOOOOOO"];        // O: 7.507
    letters = [letters stringByAppendingString:@"PP"];              // P: 1.929
    letters = [letters stringByAppendingString:@"Q"];               // Q: 0.095
    letters = [letters stringByAppendingString:@"RRRRRR"];          // R: 5.987
    letters = [letters stringByAppendingString:@"SSSSSS"];          // S: 6.327
    letters = [letters stringByAppendingString:@"TTTTTTTTT"];       // T: 9.056
    letters = [letters stringByAppendingString:@"UUU"];             // U: 2.758
    letters = [letters stringByAppendingString:@"V"];               // V: 0.978
    letters = [letters stringByAppendingString:@"WW"];              // W: 2.360
    letters = [letters stringByAppendingString:@"X"];               // X: 0.150
    letters = [letters stringByAppendingString:@"YY"];              // Y: 1.974
    letters = [letters stringByAppendingString:@"Z"];               // Z: 0.074
    return letters;
}

- (NSString*)randomLetter 
{
    
    NSMutableString *randomString = [NSMutableString stringWithCapacity: 1];
    
    NSString *letters = [self letterFrequency];
    [randomString appendFormat: @"%c", [letters characterAtIndex: arc4random()%[letters length]]];
    
    return randomString;
}



- (void)dealloc
{
    [super dealloc];
}

@end
